<template>
  <div class="mod-menu">
    <div>
      <el-button type="primary" @click="addOrUpdateHandle()">新增模板</el-button>
    </div>
    <div v-for="(item,index) in dataList">
      <div>
        <el-row class="mt30">
          <el-col :span="12">
            <div>{{item.name}} <span class="ml10">设置默认模板 <el-switch v-model="item.general" active-color="#ff4949" inactive-color="#cccccc" @change="general($event,item.id)">
                </el-switch> </span> </div>
          </el-col>
          <el-col :span="12">
            <el-button v-if="!item.general" type="danger" class="btn-compile-right" @click="deleteTemp(item)">删除</el-button>
            <el-button type="primary" class="btn-compile-right" @click="addOrUpdateHandle(item.id)">修改</el-button>
          </el-col>
        </el-row>

        <el-table :data="item.rule_list" row-key="id" border style="width: 100%;margin-top: 20px; ">
          <el-table-column prop="phone" align="center" label="可配送区域">
            <template slot-scope="scope">
              <div>
                <div v-if="scope.$index ==0">默认区域</div>
                <div v-else> {{scope.row.area_rule_literal}}
                  <span>
                    <el-button type="text" @click="addEditArea(item.id, scope.row.area_rule,item.default_rule,scope.row)">修改</el-button>
                  </span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==0" label="首件（个）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.start_standards"></el-input>
                <div v-else>{{scope.row.start_standards}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==1" label="首重量（Kg）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.start_standards"></el-input>
                <div v-else>{{scope.row.start_standards}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==2" label="首体积 （M3）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.start_standards"></el-input>
                <div v-else>{{scope.row.start_standards}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="运费（元）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.start_freight"></el-input>
                <div v-else>{{scope.row.start_freight}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==0" label="续件（个）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{scope.row.continued}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==1" label="续重量（Kg）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{scope.row.continued}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freight_compute_mode==2" label="续体积（M3）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{scope.row.continued}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="续费（元）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index !=0&&currEdit==scope.row.id" v-model="scope.row.continued_freight"></el-input>
                <div v-else>{{scope.row.continued_freight}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" v-if="scope.$index !=0" @click="edit(item.id,scope.row)">{{scope.row.id ==currEdit?'完成':'修改'}}</el-button>
              <el-button type="text" class="c-error" size="small" v-if="scope.$index !=0" @click="deleteTempRule(scope.row,item.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt30" v-if="item.free_post==false">
          <el-button type="primary" @click="addEditArea(item.id,{},item.default_rule)">点击新增配送区域</el-button>
        </div>
      </div>
    </div>

    <!--添加，编辑 -->
    <template-freight-add-update @result="updataList" v-if="addOrUpdateVisible" ref="templateFreightAddUpdate"></template-freight-add-update>
    <!--地区选择-->
    <region v-if="regionOpen" ref="region" @result='regionResult'></region>
  </div>
</template>

<script>
import region from './region'
import templateFreightAddUpdate from './template-freight-add-update'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      regionOpen: false,
      currEdit: null
    }
  },
  components: {
    templateFreightAddUpdate,
    region
  },
  mounted () {
    this.getDateList()
  },
  methods: {
    /* 地区选择返回 */
    regionResult (e) {
      this.regionOpen = false
      this.getDateList()
    },
    // 更新列表
    updataList (e) {
      this.getDateList()
    },
    // 设置默认模板
    general (e, id) {
      console.log(e, id)
      seviceApi.getFreighttemplateInfo(id).then(res => {
        if (res && res.data.code == 200) {
          res.data.data.general = e
          seviceApi.updateFreighttemplate(res.data.data).then(res => {
            if (res && res.data.code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.getDateList()
            } else {
              this.getDateList()
            }
          })
        }
      })
    },
    getDateList () {
      seviceApi.getFreighttemplateList().then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.list
        }
      })
    },
    // 添加，编辑
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.templateFreightAddUpdate.init(id || '')
      })
    },
    // 选择地区
    addEditArea (id, area_rule, default_rule, item) {
      this.regionOpen = true
      this.$nextTick(() => {
        this.$refs.region.init(id, area_rule, default_rule, item)
      })
    },

    edit (id, row) {
      if (this.currEdit != row.id) {
        // 编辑
        this.currEdit = row.id
      } else {
        // 完成
        this.currEdit = null
        seviceApi.updateFreighttemplateRule(id, row).then(res => {
          if (res.data && res.data.code == 200) {
            this.$message.success('修改成功！')
          }
        })
      }
    },

    // 删除运费模板
    deleteTemp (row) {
      this.$confirm(`是否删除模板 [ ${row.name} ]?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.deleteFreighttemplate(row.id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getDateList()
          }
        })
      }).catch(() => { })
    },

    // 商家删除运费模板规则
    deleteTempRule (row, templateId) {
      console.log(row)
      this.$confirm(`是否删除 [ ${row.area_rule_literal} ] ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = templateId + '/' + row.id
        seviceApi.deleteFreighttemplateRule(ids).then(res => {
          if (res.data && res.data.code == 200) {
            this.$message.success('删除成功！')
            this.getDateList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
<style scoped lang="scss" >
.mt30 {
  margin-top: 30px;
}

.btn-compile-right {
  float: right;
  margin-left: 10px;
}

.el-input--medium .el-input__inner {
  text-align: center;
}
</style>
